package com.zbkj.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zbkj.admin.model.dto.UserNotifyDTO;
import com.zbkj.admin.service.WeWorkService;
import com.zbkj.dal.base.enums.BooleanEnums;
import com.zbkj.dal.base.enums.EmployeeStatusEnum;
import com.zbkj.dal.base.enums.WeWorkUserUpdateEnums;
import com.zbkj.dal.dao.EmployeeDao;
import com.zbkj.dal.entity.user.Employee;
import com.zbkj.dal.feign.client.WeWorkFeignClient;
import com.zbkj.dal.feign.vo.EmployeeInfo;
import com.zbkj.service.util.XmlUtil;
import com.zbkj.service.util.wework.WXBizMsgCrypt;
import lombok.SneakyThrows;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.support.TransactionTemplate;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

@Service
public class WeWorkServiceImpl implements WeWorkService {

    @Resource
    private WXBizMsgCrypt wxBizMsgCrypt;

    @Resource
    private WeWorkFeignClient weWorkFeignClient;

    @Resource
    private EmployeeDao employeeDao;

    @Resource
    private TransactionTemplate transactionTemplate;

    @SneakyThrows
    @Override
    public String booksNotify(String msgSignature, String echostr, String nonce, String timestamp) {
        return wxBizMsgCrypt.VerifyURL(msgSignature, timestamp, nonce, echostr);
    }

    @Override
    @SneakyThrows
    public void userUpdateNotify(String msgSignature, String timeStamp, String nonce, String body) {
        String data = wxBizMsgCrypt.DecryptMsg(msgSignature, timeStamp, nonce, body);
        UserNotifyDTO userNotify = XmlUtil.xmlToBean(data, UserNotifyDTO.class);
        // 如果时新增用户，添加员工
        if (StringUtils.equals(userNotify.getChangeType(), WeWorkUserUpdateEnums.CREATE_USER.getCode())) {
            addEmployee(userNotify.getUserId());
        }
    }

    @Override
    public void initEmployee() {
        List<EmployeeInfo> employeeInfos = weWorkFeignClient.employeeList();
        for (EmployeeInfo employeeInfo : employeeInfos) {
            addEmployee(employeeInfo.getUserid());
        }
    }

    @Override
    public void addEmployee(String weWorkUserId) {
        // 查询员工存不存在，不存在添加
        LambdaQueryWrapper<Employee> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Employee::getWxWorkAccount, weWorkUserId);
        wrapper.eq(Employee::getDeleted, BooleanEnums.FALSE.getCode());
        if (Objects.isNull(employeeDao.selectOne(wrapper))) {
            String userName = weWorkFeignClient.weWorkUserName(weWorkUserId);
            transactionTemplate.execute(e -> {
                Employee employee = new Employee();
                employee.setUserName(userName);
                employee.setRealName(userName);
                employee.setWxWorkAccount(weWorkUserId);
                employee.setStatus(EmployeeStatusEnum.ENABLE.getCode());
                employee.setCreateTime(LocalDateTime.now());
                employee.setUpdateTime(LocalDateTime.now());
                employee.setDeleted(BooleanEnums.FALSE.getCode());
                employeeDao.insert(employee);
                String employeeNo = Employee.generateEmployeeNo(employee.getId());
                weWorkFeignClient.updateEmployeeAlias(weWorkUserId, employeeNo);
                employee.setEmployeeNo(employeeNo);
                employeeDao.updateById(employee);
                return true;
            });
        }
    }
}
